﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ComponentModel.Composition;
using RenderEngine;
using RenderEngineInterfaces;
using IndicoInterface.SimpleAgendaDataModel;
using DeepZoomPivotConstructor.VisualLib;
using System.Threading.Tasks;
using System.Drawing;
using IndicoRender;
using IndicoInterface;

namespace t_IndicoRender
{
    [TestClass]
    public class t_RenderIndicoAgenda
    {
        /// <summary>
        /// The agent string we use in URI requests!
        /// </summary>
        [Export("HttpAgentString")]
        private string HttpAgentString { get; set; }

        [TestInitialize]
        public void InitForTest()
        {
            HttpAgentString = "RenderIndiocWebSite Test (DeepTalk)";
            t_TestingLibrary.Utilities.MyClassInit();
            t_TestingLibrary.Utilities.AddPart(new RenderControl());
            t_TestingLibrary.Utilities.AddPart(new LayoutHelper());
            t_TestingLibrary.Utilities.AddPart(new Logging.LoggerFile());
        }

        [TestCleanup]
        public void ShutDownTest()
        {
            t_TestingLibrary.Utilities.MyClassDone();
        }

        [Import]
        private IRenderEngine MyRender { get; set; }

        /// <summary>
        /// Dummy guy to fake out rendering a meeting
        /// </summary>
        [Export(typeof(IRenderItem))]
        [RenderInfo(typeof(Meeting))]
        private class MyRenderMeeting : IRenderItem
        {
            public bool DidRender = false;
            public System.Threading.Tasks.Task<DeepZoomPivotConstructor.VisualLib.Visual> Render(object what)
            {
                return Task<Visual>.Factory.StartNew(() =>
                {
                    DidRender = true;
                    return new VisualBitmapImage(1.0, 2.0, 100, Color.Black);
                });
            }

            public RenderPluginPriority RenderPriority(object what)
            {
                return RenderPluginPriority.RPPGeneric;
            }
        }

        [TestMethod]
        public void TestRenderWebURL()
        {
            /// Can we render a url?
            t_TestingLibrary.Utilities.AddPart(this);
            MyRenderMeeting my = new MyRenderMeeting();
            t_TestingLibrary.Utilities.AddPart(my);

            RenderIndicoAgenda iw = new RenderIndicoAgenda();
            t_TestingLibrary.Utilities.Compose(iw);

            Visual v = iw.Render(new AgendaInfo("http://indico.cern.ch/conferenceDisplay.py?confId=a042880")).Result;
            Assert.AreEqual(1, v.SGNodeList.Count, "Wrong number of images came back! should be one bitmap!");
            Assert.IsTrue(my.DidRender, "Did Render didn't get called");
        }

        [TestMethod]
        public void TestRenderMetaData()
        {
            /// Can we render a url?
            MyRenderMeeting my = new MyRenderMeeting();
            t_TestingLibrary.Utilities.AddPart(my);
            t_TestingLibrary.Utilities.AddPart(this);

            var iw = new RenderIndicoAgenda();
            t_TestingLibrary.Utilities.Compose(iw);

            Visual v = MyRender.Render(new AgendaInfo("http://indico.cern.ch/conferenceDisplay.py?confId=a042880"));
            Assert.IsNotNull(v, "Should have not been null for the render return!");
            Assert.AreEqual(1, v.SGNodeList.Count, "Expected a single image!");
        }
    }
}
